package com.base.pig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.base.pig.entity.ScDevice;
import com.base.pig.mapper.ScDeviceMapper;
import com.base.pig.util.R;
import com.base.pig.service.ScDeviceService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @ProjectName
 * @Package com.pig4cloud.pig.lab.service.impl
 * @ClassName ScDeviceServiceImplServiceImpl
 * @Description 
 * @Company lab
 * @Author wangw
 * @Date 2020/4/29 0029 11:53
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@Service
@AllArgsConstructor
public class ScDeviceServiceImpl extends ServiceImpl<ScDeviceMapper, ScDevice> implements ScDeviceService {

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Page getDeviceListById(String manholeId,Long current,Long size) {
        Page page = new Page();
        QueryWrapper<ScDevice> queryWrapper = new QueryWrapper();
        if(current != null && !"".equals(current)){
            page.setCurrent(current);
        }
        if(size != null && !"".equals(size)){
            page.setSize(size);
        }
        if(manholeId != null && !"".equals(manholeId)){
            queryWrapper.eq("manhole_id",manholeId);
        }
        List<OrderItem> orders = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("gmt_create");
        orderItem.setAsc(false);
        orders.add(orderItem);
        page.setOrders(orders);
        return this.page(page,queryWrapper);
    }

    @Override
    public R getDeviceSn(String deviceType, String sn) {
        QueryWrapper snWrapper = new QueryWrapper();
        snWrapper.select("distinct sn");
        snWrapper.eq("device_type",deviceType);
        snWrapper.like("sn",sn);
        List<ScDevice> list = this.list(snWrapper);
        List<String> snList = list.stream().map(scDevice -> {
            return scDevice.getSn();
        }).collect(Collectors.toList());

        return R.ok(snList,"查询完成.");
    }

    @Override
    public R getDeviceByType(String deviceType) {
        QueryWrapper snWrapper = new QueryWrapper();
        snWrapper.select("device_id","sn");
        snWrapper.eq("device_type",deviceType);
        List<ScDevice> list = this.list(snWrapper);
        List<Map> snList = list.stream().map(scDevice -> {
            Map<String,Object> map = new HashMap<>();
            map.put("deviceId",scDevice.getDeviceId());
            map.put("sn",scDevice.getSn());
            return map;
        }).collect(Collectors.toList());

        return R.ok(snList,"查询完成.");
    }
}
